package com.citywithincity.db.sync;

import java.util.List;

import org.apache.http.util.TextUtils;

import com.citywithincity.db.DBException;
import com.citywithincity.db.Db;
import com.citywithincity.db.Record;
import com.citywithincity.db.interfaces.Ar;
import com.citywithincity.utils.StringUtils;

/**
 * 同步流程
 * @author renxueliang
 *
 */
public class SyncProgressAll implements ISyncData {
	
	private int srcDb;
	private String srcTable;
	private String[] srcFields;
	private SyncFieldsContainer container;
	private SyncInsertMethod insert;
	private SyncCondition condition;
	
	private String extraFields;
	
	@Override
	public void destroy() {
		container.destroy();
		container = null;
		
		condition = null;
		insert = null;
		
	}
	public SyncProgressAll(int srcDb,String srcTable,String[] srcFields,SyncFieldsContainer container,SyncInsertMethod insert,SyncCondition condition,String extraFields){
		this.condition = condition;
		this.container = container;
		this.insert = insert;
		this.srcDb = srcDb;
		this.srcFields = srcFields;
		this.srcTable = srcTable;
		this.extraFields = extraFields;
	}
	
	
	
	public void syncData() throws DBException{
		condition.init();
		String f = StringUtils.join(srcFields);
		if(!TextUtils.isEmpty(extraFields)){
			f = f + "," + extraFields;
		}
		Ar ar = Db.ar(srcDb).select(f);
		condition.setAr(ar);
		List<Record> list = ar.get(srcTable);
		container.syncList( insert, list);
	}



	
	
}
